﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNA_Game_Framework
{
	public class DialogueAuto : Container
	{
		public enum Buttons { YesNo, OkCancel };

		Theme.Dialogue dialogueTheme;

		public DialogueAuto(String name, object parent, Vector2 position, String text, Buttons buttons, Theme.Dialogue dialogueTheme)
			: base(name, parent, position, new Vector2(0, 0))
		{
			this.dialogueTheme = dialogueTheme;

			SetSizeBasedOnText(text);
			size = AutoSizeTools.AdjustSizeToTextures(size,dialogueTheme.BorderTheme,dialogueTheme.Background);
			CreateBackground();
			CreateBorder();
			CreateLabel(text);
			CreateButtons(buttons);
		}

		private void SetSizeBasedOnText(string text)
		{
			Vector2 textSize = dialogueTheme.DialogueFont.MeasureString(text);

			int width = (int)(dialogueTheme.LeftBorder.Width + dialogueTheme.RightBorder.Width + textSize.X + 60);
			int height = (int)(dialogueTheme.TopBorder.Height + dialogueTheme.BottomBorder.Height + textSize.Y + dialogueTheme.ButtonTexture.Height + 60);
			size = new Vector2(width, height);
		}

		private void AdjustSizeToTextures()
		{
			AdjustWidthToTextures();
			AdjustHeightToTextures();
		}

		public void AdjustWidthToTextures()
		{
			int newWidth = this.Width;
			
			int topwidthnocorners = newWidth - dialogueTheme.TopLeftBorder.Width - dialogueTheme.TopRightBorder.Width;
			int bottomwidthnocorners = newWidth - dialogueTheme.BottomLeftBorder.Width - dialogueTheme.BottomRightBorder.Width;
			int backgroundnoborders = newWidth - dialogueTheme.LeftBorder.Width - dialogueTheme.RightBorder.Width;
			
			if (topwidthnocorners % dialogueTheme.TopBorder.Width != 0 || bottomwidthnocorners % dialogueTheme.BottomBorder.Width != 0 || backgroundnoborders % dialogueTheme.Background.Width != 0)
			{
				int currentAmountOfWholeSlices = (backgroundnoborders) / dialogueTheme.Background.Width;
				int newAmountOfSlices = currentAmountOfWholeSlices;

				int tempNewWidth = 0;
				while (tempNewWidth != newWidth)
				{
					newAmountOfSlices++;
					tempNewWidth = newAmountOfSlices * dialogueTheme.Background.Width + dialogueTheme.LeftBorder.Width + dialogueTheme.RightBorder.Width;

					int tempNewWidthNoTopCorner = tempNewWidth - dialogueTheme.TopLeftBorder.Width - dialogueTheme.TopRightBorder.Width;
					int tempNewWidthNoBottomCorner = tempNewWidth - dialogueTheme.BottomLeftBorder.Width - dialogueTheme.BottomRightBorder.Width;
					int tempNewWidthNoBorders = tempNewWidth - dialogueTheme.LeftBorder.Width - dialogueTheme.RightBorder.Width;

					if (tempNewWidthNoTopCorner % dialogueTheme.TopBorder.Width == 0 && tempNewWidthNoBottomCorner % dialogueTheme.BottomBorder.Width == 0 && tempNewWidthNoBorders % dialogueTheme.Background.Width == 0)
					{
						newWidth = tempNewWidth;
					}
				}
			}

			size.X = newWidth;
		}

		public void AdjustHeightToTextures()
		{
			int newHeight = this.Height;

			int leftheightnocorners = newHeight - dialogueTheme.TopLeftBorder.Height - dialogueTheme.BottomLeftBorder.Height;
			int rightheightnocorners = newHeight - dialogueTheme.TopRightBorder.Height - dialogueTheme.BottomRightBorder.Height;
			int backgroundnoborders = newHeight - dialogueTheme.TopBorder.Height - dialogueTheme.BottomBorder.Height;

			if (leftheightnocorners % dialogueTheme.LeftBorder.Height != 0 || rightheightnocorners % dialogueTheme.RightBorder.Height != 0 || backgroundnoborders % dialogueTheme.Background.Height != 0)
			{
				int currentAmountOfWholeSlices = (backgroundnoborders) / dialogueTheme.Background.Height;
				int newAmountOfSlices = currentAmountOfWholeSlices;

				int tempNewHeight = 0;
				while (tempNewHeight != newHeight)
				{
					newAmountOfSlices++;
					tempNewHeight = newAmountOfSlices * dialogueTheme.Background.Height + dialogueTheme.TopBorder.Height + dialogueTheme.BottomBorder.Height;

					int tempNewHeightNoLeftCorner = tempNewHeight - dialogueTheme.TopLeftBorder.Height - dialogueTheme.BottomLeftBorder.Height;
					int tempNewHeightNoRightCorner = tempNewHeight - dialogueTheme.TopRightBorder.Height - dialogueTheme.BottomRightBorder.Height;
					int tempNewHeightNoBorders = tempNewHeight - dialogueTheme.TopBorder.Height - dialogueTheme.BottomBorder.Height;

					if (tempNewHeightNoLeftCorner % dialogueTheme.LeftBorder.Height == 0 || tempNewHeightNoRightCorner % dialogueTheme.RightBorder.Height == 0 || tempNewHeightNoBorders % dialogueTheme.Background.Height == 0)
					{
						newHeight = tempNewHeight;
					}
				}
			}

			size.Y = newHeight;
		}


		private void CreateBackground()
		{
			this.AddTileImage("", new Vector2(dialogueTheme.LeftBorder.Width, dialogueTheme.TopBorder.Height), dialogueTheme.Background, new Vector2(size.X - dialogueTheme.LeftBorder.Width - dialogueTheme.RightBorder.Width, size.Y - dialogueTheme.TopBorder.Height - dialogueTheme.BottomBorder.Height));
		}

		private void CreateBorder()
		{
			this.AddBorder("", new Vector2(0, 0), this.size, Border.Sides.All, dialogueTheme.BorderTheme.Name);
		}

		private void CreateLabel(string text)
		{
			int buttonTop = (int)(Size.Y - dialogueTheme.BottomBorder.Height - 20 - dialogueTheme.ButtonTheme.buttonTexture.Height);
			int halfLineSeperation = (int)(Size.Y - dialogueTheme.TopBorder.Height - buttonTop - dialogueTheme.DialogueFont.MeasureString("pl").Y) / 2;
			Vector2 linePosition = new Vector2(dialogueTheme.TopLeftBorder.Width + 30, dialogueTheme.TopBorder.Height + halfLineSeperation);

			this.AddLabel("", linePosition, text, dialogueTheme.DialogueFont);
		}

		private void CreateButtons(Buttons buttons)
		{
			switch (buttons)
			{
				case Buttons.YesNo:
					CreateYesNoButtons();
					break;
				case Buttons.OkCancel:
					CreateOkCancelButtons();
					break;
				default:
					break;
			}
		}

		private void CreateYesNoButtons()
		{
			Button yesButton = CreateSingleButton("Yes", "Yes", dialogueTheme.ButtonTheme);
			Button noButton = CreateSingleButton("No", "No", dialogueTheme.ButtonTheme, yesButton);

			if ((yesButton.Width + noButton.Width + 60) > Size.X)
			{
				size.X = yesButton.Width + noButton.Width + 60;
			}
		}

		private void CreateOkCancelButtons()
		{
			Button okButton = CreateSingleButton("Ok", "OK", dialogueTheme.ButtonTheme);
			Button cancelButton = CreateSingleButton("Cancel", "Cancel", dialogueTheme.ButtonTheme, okButton);

			if ((okButton.Width + cancelButton.Width + 60) > Size.X)
			{
				size.X = okButton.Width + cancelButton.Width + 60;
			}
		}

		private Button CreateSingleButton(string buttonName, string buttonText, Theme.Button theme, Button lastButton = null)
		{
			Button button = new Button(this, "btn" + buttonName + "@" + name, new Vector2(0, 0), buttonText, theme);
			Vector2 buttonPosition;
			if (lastButton != null)
			{
				buttonPosition = lastButton.WindowPosition + new Vector2(lastButton.Width + 20, 0);
			}
			else
			{
				buttonPosition = new Vector2(20, this.Height - 20 - button.Height);
			}
			button.WindowPosition = buttonPosition;
			button.Click += CloseDialogue;
			controls.AddControl(button);
			return button;
		}

		private void CloseDialogue(object sender, EventArgs e)
		{
			((Screen)parent).RemoveContainer(name);
		}

		#region Region Draw
		internal override void Draw(GraphicsDevice graphics, SpriteBatch spriteBatch)
		{
			if (Visible)
			{
				DrawControls(graphics, spriteBatch);
				DrawSubWindows(graphics, spriteBatch);
			}
		}
		#endregion
	}
}
